#include <iostream>
#include <pthread.h>
#include <unistd.h>

__thread int g_val = 100;

std::string toHex(pthread_t tid)
{
    char buff[64];
    snprintf(buff, sizeof buff, "0x%lx", tid);
    return buff;
}

void* routine1(void* args)
{
    std::string name = static_cast<char*>(args);
    while (1)
    {
        printf("my name is: %s, my id is: %s, g_val: %d, &g_val: %p\n", 
            name.c_str(), toHex(pthread_self()).c_str(), g_val, &g_val);
        sleep(1);
    }
    return nullptr;
}

void* routine2(void* args)
{
    std::string name = static_cast<char*>(args);
    while (1)
    {
        printf("my name is: %s, my id is: %s, g_val: %d, &g_val: %p\n", 
            name.c_str(), toHex(pthread_self()).c_str(), g_val, &g_val);
        g_val++;
        sleep(1);
    }
    return nullptr;
}


int main()
{
    pthread_t tid1, tid2;
    pthread_create(&tid1, nullptr, routine1, (void*)"thread-1"); 
    pthread_create(&tid2, nullptr, routine2, (void*)"thread-2"); 
    
    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    return 0;
}